<?php
namespace other\orders\fx\format;

// 格式化参数
use other\ShopingConn;
use other\Table;

class Airwallex
{
    // 类别（int）
    static $abnormal_orders = 0;

    // 授权失败的code
    static public $dic = [
        '59' => '有欺诈嫌疑',
        '83' => '发卡行拒绝了这笔交易。请参考原始响应代码',
        '51' => '余额不足或超出信用额度',
        '100' => 'the card issuer declined this transaction. please refer to the original response code.100',
        '122' => 'the card issuer declined this transaction. please refer to the original response code.122',
        '79' => 'the card issuer declined this transaction. please refer to the original response code.79',
        '82' => '发卡行政策问题',
        '62' => '受限卡片（在此地区或国家无效）',
        '61' => '取款金额超限',
        '05' => '不予承兑',
        '57' => '不允许发卡行/持卡人进行的交易',
        'N7' => 'CVV2 验证失败，交易被拒绝',
        '12' => '交易无效',
        '54' => '卡过期或有效期缺失',
        'ZWF' => '未定义',
    ];



    static public $names = [
        'payment_intent.created' => '已创建',
        'payment_intent.requires_payment_method' => '要求付款方式',
        'payment_attempt.received' => '处理中',
        'payment_attempt.authentication_redirected' => '重定向授权中',
        'payment_intent.requires_customer_action' => '待客户操作',

        'payment_attempt.authorization_failed' => '授权失败',
        'payment_attempt.authorized' => '已授权',


        'payment_attempt.authentication_failed' => '无法验证付款方式',
        'payment_attempt.expired' => '付款过期',
        'payment_attempt.paid' => '已经成功到商家的钱包',
        'payment_attempt.settled' => '已收到资金',
        'payment_attempt.cancelled' => '已取消订单',
        'payment_attempt.failed_to_process' => '无法处理付款',

        'payment_intent.succeeded' => '成功',
        'payment_attempt.risk_declined' => '未通过风险筛选',

//        'payment_attempt.capture_requested' => '处理拒付申请',
        'payment_dispute.requires_response' => '待回复',
        'payment_dispute.accepted' => '已接受',
    ];


    /**
     * 数组排序
     * @param $arr
     * @return array
     */
    static public function order($arr){

        return $arr;

        $need = [];
        foreach (self::$names as $k => $v){
            if (isset($arr[$k])){
                $need[] = $arr[$k];
            }
        }

        return $need;
    }

    /**
     * 拒付
     * @param $param
     * @return string
     */
    static public function payment_dispute_accepted($params){
        return '';

        $msg = '';
        $stage = empty($params['data']['object']['stage'])?'':$params['data']['object']['stage'];
        $type = empty($params['data']['object']['reason']['type'])?'':$params['data']['object']['reason']['type'];

        if ($stage == 'PRE_CHARGEBACK' || $stage == 'CHARGEBACK'){
            $msg .= '已拒付,';
        }

        if ($type){
            $msg .= '用户申请拒付原因："'.$type.'"';
        }


        if ($msg) $msg = '('.$msg.')';
        return $msg;
    }


    // 需要响应
    static public function payment_dispute_requires_response($params){
        $msg = '';
        $stage = empty($params['data']['object']['stage'])?'':$params['data']['object']['stage'];
        $type = empty($params['data']['object']['reason']['type'])?'':$params['data']['object']['reason']['type'];

        if ($stage == 'PRE_CHARGEBACK' || $stage == 'CHARGEBACK'){
            $msg .= '已拒付,';
        }

        if ($type){
            $msg .= '用户申请拒付原因："'.$type.'"';
        }


        if ($msg) $msg = '('.$msg.')';
        return $msg;
    }


    // 授权失败
    static public function payment_attempt_authorization_failed($params){
        $payment_intent_id = empty($params['data']['object']['payment_intent_id'])?'':$params['data']['object']['payment_intent_id'];

        // 获取授权失败的code
        $provider_original_response_code = empty($params['data']['object']['provider_original_response_code'])?'ZWF':$params['data']['object']['provider_original_response_code'];

//        if (!in_array($provider_original_response_code,self::$all)){
//            self::$all[] = $provider_original_response_code;
//        }
//        var_dump($provider_original_response_code.'--'.$payment_intent_id);


        $msg = empty(self::$dic[$provider_original_response_code]) ? $provider_original_response_code : self::$dic[$provider_original_response_code];
        if ($msg) $msg = '('.$msg.')';
        return $msg;
    }


    // 未通过风险筛选
    static public function payment_attempt_risk_declined($params){

        $msg = empty($params['data']['error']['message'])?'':$params['data']['error']['message'];
        if ($msg) $msg = '('.$msg.')';
        return $msg;
    }

    static public $all = [];
}
